package com.guigu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.mapper.GatherMapper;
import com.guigu.pojo.Gather;
import com.guigu.pojo.Pay;
import com.guigu.service.GatherService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * <p>
 * 入库单表 服务实现类
 * </p>
 *
 * @author Mr.Zhang
 * @since 2025-03-12
 */
@Service
public class GatherServiceImpl extends ServiceImpl<GatherMapper, Gather> implements GatherService {

    @Autowired
    private GatherMapper gatherMapper;

    @Override
    public Page<Gather> gatherReview(HashMap<String, Object> map) {
        Integer currentPage = 1;
        Integer pageSize = 5;

        Object pageNumObj = map.get("currentPage");
        if (pageNumObj != null) {
            if (pageNumObj instanceof String && StringUtils.isNotBlank((String) pageNumObj)) {
                currentPage = Integer.parseInt((String) pageNumObj);
            } else if (pageNumObj instanceof Integer) {
                currentPage = (Integer) pageNumObj;
            }
        }
        Object pageSizeObj = map.get("pageSize");
        if (pageSizeObj != null) {
            if (pageSizeObj instanceof String && StringUtils.isNotBlank((String) pageSizeObj)) {
                pageSize = Integer.parseInt((String) pageSizeObj);
            } else if (pageSizeObj instanceof Integer) {
                pageSize = (Integer) pageSizeObj;
            }
        }

        Page<Gather> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Gather> queryWrapper = new QueryWrapper<>();
        if (map.get("CheckTage") != null && StringUtils.isNotBlank((String) map.get("CheckTage"))) {
            queryWrapper.eq("check_tag", map.get("CheckTage"));
        }

        Page<Gather> payPage = gatherMapper.selectPage(page, queryWrapper);
        return payPage;
    }

    @Override
    public Page<Gather> gatherReview2(HashMap<String, Object> map) {
        Integer currentPage = 1;
        Integer pageSize = 5;

        Object pageNumObj = map.get("currentPage");
        if (pageNumObj != null) {
            if (pageNumObj instanceof String && StringUtils.isNotBlank((String) pageNumObj)) {
                currentPage = Integer.parseInt((String) pageNumObj);
            } else if (pageNumObj instanceof Integer) {
                currentPage = (Integer) pageNumObj;
            }
        }
        Object pageSizeObj = map.get("pageSize");
        if (pageSizeObj != null) {
            if (pageSizeObj instanceof String && StringUtils.isNotBlank((String) pageSizeObj)) {
                pageSize = Integer.parseInt((String) pageSizeObj);
            } else if (pageSizeObj instanceof Integer) {
                pageSize = (Integer) pageSizeObj;
            }
        }

        Page<Gather> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Gather> queryWrapper = new QueryWrapper<>();
        if (map.get("outboundId") != null && StringUtils.isNotBlank((String) map.get("outboundId"))) {
            queryWrapper.like("gather_order_number", map.get("outboundId"));
        }
        if (map.get("status") != null && StringUtils.isNotBlank((String) map.get("status"))) {
            queryWrapper.like("check_tag", map.get("status"));
        }

        Page<Gather> payPage = gatherMapper.selectPage(page, queryWrapper);
        return payPage;
    }

    @Override
    public int addGather(Gather gather) {

        int insert = gatherMapper.insert(gather);
        System.out.println(insert);
        return gather.getGatherId();
    }

    @Override
    public Gather getGatherBygatherOrderNumber(String gatherOrderNumber) {
        Gather gatherOrderNumber1 = gatherMapper.selectOne(new QueryWrapper<Gather>().eq("gather_order_number", gatherOrderNumber));

        System.out.println(gatherOrderNumber1);
        return gatherOrderNumber1;
    }
}
